﻿using DapperExtensions;
using KD.Service.HR.Utility;
using Logistics.Model;
using Logistics.Model.QueryModel;
using Logistics.Service;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace Logistics.WebApi.Controllers
{
    /// <summary>
    /// 签约授权
    /// </summary>
    [MyAuthority]
    public class WLCompanyBusinessController : ApiController
    {
        private WuliuBusinessService service = new WuliuBusinessService();
        private ContractService contractService = new ContractService();
        private CompanyService companyService = new CompanyService();
        [HttpPost]
        /// <summary>
        /// 企业向物流签约列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResponseModel GetList(WLCompanyQuery query)
        {
            var resultmod = new ResponseModel();

            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                int company_id = companyService.GetCompanyIdByAccountId(accountId);
                var list = new ContractAuthorizatQuery();
                if (!string.IsNullOrEmpty(query.companyname))
                    list.wuliu_name = query.companyname;
                if (!string.IsNullOrEmpty(query.cityid))
                    list.city_id = (query.cityid).ToInt32();
                if (company_id > 0)
                    list.company_id = company_id;
                resultmod.mod = service.GetList(list, query.page - 1, query.pagesize);
                resultmod.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 通过物流端发启请求进行审核，需要更新企业签约表状态
        /// </summary>
        /// <param name="apply"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel UpdateApply(wuliu_company_apply_query apply)
        {
            var responseModel = new ResponseModel();
            try
            {

                if ("20" == apply.apply_flag)
                {
                    var contract = new wuliu_contract_base();
                    contract.wuliu_id = apply.wuliu_id;
                    contract.company_id = apply.company_id;
                    contract.apply_flag = "10";//签约标志（10 企业同意
                    contract.contract_flag = "1";//签约标志（1 签约
                    contract.contract_state = "10";//状态（1 停用 10 启用）

                    if (contractService.IsExitNum(contract))
                    {
                        contractService.Add(apply);
                        var wlBusiness = service.GetWLBusinessByWuliuId((int)apply.wuliu_id);
                        if (wlBusiness == null || wlBusiness.id == 0)
                        {
                            wlBusiness = new wuliu_wlcompany_business();
                            wlBusiness.sign_num = 1;
                            wlBusiness.wuliu_id = apply.wuliu_id;
                            wlBusiness.wuliu_name = apply.wuliu_name;
                            wlBusiness.wuliu_no = apply.wuliu_no;
                            service.Add(wlBusiness);
                        }
                        else
                        {
                            wlBusiness.sign_num++;
                            service.Update(wlBusiness);
                        }
                        responseModel.mod = service.UpdateFlag(apply.id, "20");
                    }
                    else
                    {
                        responseModel.mod = service.UpdateFlag(apply.id, "20");
                    }

                }

                responseModel.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }


        /// <summary>
        /// 申请不通过
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel NoApply(int applyid)
        {
            ResponseModel model = new ResponseModel();
            var flag = service.UpdateFlag(applyid, "10");
            if (flag)
                model.InitResultState(UpdateStateEnum.Success);
            else
                model.InitResultState(UpdateStateEnum.Error);
            return model;

        }


    }
}